package apis

import (
	"fmt"
	"google.golang.org/protobuf/proto"
	"mmo_game_zinx/core"
	"mmo_game_zinx/pb"
	"zinx/ziface"
	"zinx/znet"
)

// 世界聊天的路由业务
type WorldChat struct {
	znet.BaseRouter
}

func (wc *WorldChat) Handle(request ziface.IRequest) {
	// 解析客户端传递过来的proto协议
	talkMsg := &pb.Talk{}
	err := proto.Unmarshal(request.GetData(), talkMsg)
	if err != nil {
		fmt.Println("talk unmarshal error:", err)
		return
	}

	// 当前的聊天数据是属于哪个玩家添加到WorldManager中
	pid, err := request.GetConnection().GetProperty("pid")
	if err != nil {
		fmt.Println("not get player id from conn; err: ", err)
		return
	}

	// 通过pid获得玩家Player对象
	player := core.WorldMgrObj.GetPlayerByPid(pid.(int32))

	//将这个消息广播给其他在线玩家
	player.Talk(talkMsg.Content)

}
